/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.practica.dao;

import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import edu.practica.model.Detalle;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Algo
 */
@Stateless
public class DetalleDAO {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
    @PersistenceContext
    private EntityManager entityManager;


    @PostConstruct
    public void init() {
        System.out.println("entityManager: "+entityManager);
    }
    

    public void registar(Detalle detalle) {
        System.out.println("DetalleDAO.registar:" + detalle);
        entityManager.persist(detalle);
    }
    
    public void eliminar(Detalle detalle) {
        System.out.println("Zaona_BarrioDAO.eliminar:" + detalle);
        detalle = entityManager.getReference(Detalle.class, detalle.getId());
        entityManager.remove(detalle);
    }
    
    public void modificar(Detalle detalle) {
        System.out.println("ProductoDAO.modificar:" + detalle);
        entityManager.merge(detalle);
    }
    
    public Detalle findByID(Integer id) {
        return entityManager.find(Detalle.class, id);
    }    

    public List<Detalle> listDetalle() {
        List<Detalle> list = null;
        
        TypedQuery<Detalle> query = 
                entityManager.createQuery("SELECT p FROM Detalle p", Detalle.class);
        list = query.getResultList();
        
        return list;
    }
    
    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}
